<?php
namespace App\Http\Service;

use App\Models\Userreport;
use App\Models\Baoliaopost;
use App\Models\Doubanpost;
use App\Http\Service\UserService;

use App\Http\Service\BaseService;
use App\Http\Service\ReporttypeconfigService;
use Illuminate\Support\Facades\Config;
class UserreportService  extends BaseService
{
    private $model;
    private $getUserInfo;
    public function __construct(Userreport $model,Baoliaopost $baoliaopost,Doubanpost $doubanpost,ReporttypeconfigService $reporttypeconfigService,UserService $userService)
    {
        parent::__construct();
        $this->model = $model;
        $this->baoliaopost=$baoliaopost;
        $this->doubanpost=$doubanpost;
        $this->reporttypeconfigService=$reporttypeconfigService;
        $this->userService=$userService;
        $this->url=config('myurl.link_parse');
        $this->getUserInfo=config('myurl.getUserInfo');

       
    }

    #我的帖子举报列表
    public function getList($request,$data=[])
    {
        $page = intval($request->json('page')) ?$request->json('page') :$this->cpage;
        $limit = intval($request->json('limit')) ?$request->json('limit') :$this->climit;
        $offset = ($page-1)*$limit;
        $reporttypeconfig=$this->reporttypeconfigService->reportconfiglist($request);

        $type=$data['type'];

        $where=[
            'A.user_id'=>$data['user_id']
        ];
        if($type=='1'){
            #1. 豆瓣(逛逛)
            $count = $this->model->from('user_report as A') // 主表使用别名
            ->join('douban_post as B', 'A.unique_id', '=', 'B.unique_id') // 关联表使用别名
            ->where($where)->count();
            $fields = ['A.*','B.title'];
            $list = $this->model->select($fields)
                ->from('user_report as A') // 主表使用别名
                ->join('douban_post as B', 'A.unique_id', '=', 'B.unique_id') // 关联表使用别名
                ->where($where)
                ->offset($offset)->limit($limit)
                ->orderBy('A.update_time', 'desc')
                ->get();


        }else{
            #2. 爆料
            $count = $this->model->from('user_report as A') // 主表使用别名
            ->join('baoliao_post as B', 'A.post_unique_id', '=', 'B.unique_id') // 关联表使用别名
            ->where($where)->count();
            $fields = ['A.user_id','A.report_type','A.report_reason','A.report_evidence','A.report_status'];
            #$this->getLastSql();
            $list = $this->model->select($fields)
                ->from('user_report as A') // 主表使用别名
                ->join('baoliao_post as B', 'A.post_unique_id', '=', 'B.unique_id') // 关联表使用别名
                ->where($where)
                ->offset($offset)->limit($limit)
                ->orderBy('A.update_time', 'desc')
                ->get();
            #dd($list);
        }

        foreach ($list as $k => &$v) {
            $report_type=$v['report_type'];
            $config_data=$reporttypeconfig[$report_type];
            $v['description']=$config_data['description'];
            $report_status=$v['report_status'];
            $v['report_status_desc']=status_desc($report_status);
            #dd($v['report_status_desc']);
            $user_id=$v['user_id'];
            $user=$this->userService->update_user($user_id);
            $value['nickname']=$user['nickname'];
            $value['avatar']=$user['avatar'];
            
        }
        $result = [
            'page'=> $page,
            'limit'=> $limit,
            'total_page'=> ceil($count/$limit),
            'count'=>$count,
            'data'=> $list
        ];
        return $result; 
        
    }

    /**
     *
     * @param array $params
     * @return array
     */
    public function addData($request,$data=[])
    {
        $type=$data['type'];
        $where=[
            'unique_id'=>$data['post_unique_id']
        ];
        if($type==1){
            #1. 豆瓣(逛逛)  
            $first=$this->doubanpost->where($where)->first();
        }else{
            #2.爆料
            $first=$this->baoliaopost->where($where)->first();
        }
        if(empty($first)){
            $this->error("帖子不存在");
        }

        
        $data['create_time']=date("Y-m-d H:i:s");
        $data['update_time']=date("Y-m-d H:i:s");
        //dd($data);
        $insertId=$this->model->insertGetId($data);  

        return true;
        
        
    }

     //删除用户举报的帖子
    public function del($request,$data=[],$ids)
    {
        $res =  $this->model->whereIn('id',$ids)->delete();
        if(!$res){
            $this->error('删除失败');
        }else{
            $this->success();
        }
    }   

   
}
